
import { Component, OnInit, ElementRef, AfterViewInit, ViewChild } from '@angular/core';
import { CommService } from '../../comm/comm.service';
import { ReportUrls } from '../../comm/report.urls.conf';
import { SubArea } from '../../model/report/sub-area';
import { Resource } from '../../model/report/resource';
import { QueryModel } from '../../model/report/query-model';
import { ChartOptionCreater } from '../../model/comm/chart-option-creater';
import { Shift } from '../../model/report/shift';
declare const $: any;
import * as Enumerable from 'linq';
import { TVResource } from 'app/ptpmc/model/comm/tv-resource';
import { DxDropDownBoxComponent } from 'devextreme-angular';
import { TipPtrSummary } from 'app/ptpmc/model/report/tip-ptr-summary';
import { NgForm } from '@angular/forms';
import { FormsModule } from '@angular/forms';

@Component({
    selector: 'app-report-pun',
    templateUrl: './pun.component.html',
    styleUrls: ['./pun.component.scss']
})

export class PunComponent implements OnInit {
    title = 'PUN';
    public punList: any[];
    /*生产线 - 班次*/
    public subAreaItems: SubArea[];
    public equipemntItems: TVResource[] = [];
    public subAareSelected: any;
    public equipmentSelected: TVResource[];
    //public isSPC = false;
    public punLike;

    @ViewChild('dddb') dddb: DxDropDownBoxComponent;
    clearEquipmentSelected() {
        this.equipmentSelected = [];
    }

    constructor(private commServices: CommService) {
    }

    ngOnInit() {
        this.commServices.initDatePicker($('#DateTimeRange'), 'YYYY-MM-DD HH:mm');
        this.subAreaItems = this.commServices.getSubAreas();
        this.subAareSelected = this.subAreaItems[0];
       // if(this.isSPC = false){
            this.equipemntItems = this.commServices.getTVResourceBySubArea(this.subAreaItems[0].subArea);
        // } else{
        //     this.equipemntItems = this.commServices.getSPCResourceBySubArea(this.subAreaItems[0].subArea);
        // }
        
       // this.equipmentSelected = this.equipemntItems;
        this.equipmentSelected = [];
    }

    equipmentSelectedOk() {
        this.dddb.instance.close();
    }

    onSubAreaSelectedChange(subArea: any): void {
        //if(this.isSPC = false){
            this.equipemntItems = this.commServices.getTVResourceBySubArea(subArea.value.subArea);
        // } else{
        //     this.equipemntItems = this.commServices.getSPCResourceBySubArea(subArea.value.subArea);
        // }
       
        this.equipmentSelected = this.equipemntItems;
    }

    getReportData(): void {
        const queryModel: QueryModel = new QueryModel();
        queryModel.dateTimeRange = this.getTime('DateTimeRange');
        queryModel.querySubAreaIds = [this.subAareSelected.subArea];
        queryModel.queryResourceIds = this.equipmentSelected.map(e => e.resource);
        if (this.punLike !== '' && this.punLike !== undefined) {
            queryModel.tag = this.punLike;
        }
        this.commServices
            .getList(queryModel, ReportUrls.PunList)
            .subscribe(model => {
                if (model.resultCode === 200) {
                    if(queryModel.querySubAreaIds[0].slice(0,2) === 'GF'){
                        model.data.forEach(e => {
                            var sub: string = '';
                            sub = e.subArea.replace(/(^\s*)|(\s*$)/g, "");
                            switch(sub){
                                case 'GFAL1': sub += '_装配主线';  break;
                                case 'GFBU1': sub += '_测试线';  break;
                                case 'GFCL1': sub += '_CASE 机加工线';  break;
                                case 'GFCR1': sub += '_行星齿轮组分装线';  break;
                                case 'GFCS1': sub += '_中间支撑架分装线';  break;
                                case 'GFDH1': sub += '_飞轮盘减震器壳体分装线';  break;
                                case 'GFHL1': sub += '_HOUSING 机加工线';  break;
                                case 'GFTC1': sub += '_壳体分装线';  break;
                                case 'GFTD1': sub += '_中间轴&差速器分装线';  break;
                            }
                            e.subArea = sub;
                        });
                    }
                    this.punList = model.data;
                } else {
                    console.log(model.resultCode + ',' + model.resultMessage);
                }
            });
    }

    getTime(id: any): String {
        const timerange: String = $('#' + id).val();
        return timerange;
    }

    onExporting(e) {
        e.fileName = e.fileName + new Date().toISOString().replace(/[\-\:\.]/g, '').split('T')[0];
    }
}
